<?php

/**
 *  Description of Examine
 *
 * @author LJ <jun.lu.726@gmail.com>
 * @copyright 2012-2013 shangbox.com
 * $Id: Examine.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 * @history    2012-11-6  18:17:51::  Lujun  ::  Create File
 */
class Controller_Ad_Examine extends Controller_Abstract
{

    public function init()
    {
        parent::init();
    }

    /**
     * 审核列表页
     */
    public function indexAction()
    {
        // 初始
        $multipage = $list      = $beginTime = $endTine   = $err       = '';
        $params    = array(
            'key_word'   => '',
            'date_type'  => '',
            'begin_time' => '',
            'end_time'   => '',
        );
        $whereArr    = array('state' => 1);
        $state  = 0;

        // PAGE start pagesize
        $page     = max(1, $this->getInt('page'));
        $pageSize = 1;
        $start    = $page * $pageSize - $pageSize;

        $a = $this->getInt('a');

        if ($a) {
            $dateType = $this->getInt('date_type');
            if (in_array($dateType, array(1, 3, 5))) {
                $endTine   = date('Y-m-d');
                $beginTime = date('Y-m-d', strtotime("-" . $dateType . " day"));
            } else {
                // 结束时间
                $endTine = $this->get('end_time');
                if ($endTine) {
                    if (!Com_Validate::date($endTine)) {
                        $err       = '请正确输入结束日期';
                    }
                }
                // 开始时间
                $beginTime = $this->get('begin_time');
                if (!Com_Validate::date($beginTime)) {
                    $err     = '请正确输入开始日期';
                }
            }
            $keyWord = $this->getx('key_word');

            $params = array(
                'key_word'   => $keyWord,
                'date_type'  => $dateType,
                'begin_time' => $beginTime,
                'end_time'   => $endTine,
                'a'          => $a,
            );
            if (in_array($dateType, array(1, 3, 5))) {
                $params['begin_time'] = '';
                $params['end_time']   = '';
            }
        }

        // ad model object
        $ad = Model('Ad_Ad');

        // 条件

        $count = $ad->getCount($err, strtotime($beginTime), strtotime($endTine), $state);
        if ($count > 0) {
            $list = $ad->getList($err, strtotime($beginTime), strtotime($endTine), $state, $start, $pageSize);
        }

        $multipage = Com_Pager::admincp($count, $pageSize, $page, '/_ad/examine', $params);

        $this->assign("list", $list);
        $this->assign("multipage", $multipage);
        $this->assign("params", $params);
        $this->assign("err", $err);
    }

    /**
     * 查看单条广告
     */
    public function viewOneAction()
    {
        $adId = $this->getInt('ad_id');
        if ($adId < 1) {
            $this->alert('没有这个活动!', 'err', '/_ad');
        }

        $ad       = Model('Ad_Ad');
        $baseInfo = $ad->row(array('ad_id' => $adId));

        $appType = Model('Ad_AppType');
        $appInfo = $appType->row(array('ad_id' => $adId));

        $field     = Model('Ad_Field');
        $fieldInfo = $appType->row(array('ad_id' => $adId));
        $this->assign("baseInfo", $baseInfo);
        $this->assign("appInfo", $appInfo);
        $this->assign("fieldInfo", $fieldInfo);
    }

    /**
     * 审核 弹出框
     */
    public function examineWindowAction()
    {
        $adId = $this->getInt('ad_id');
        if ($adId < 1) {
            $this->alert('没有这个活动!', 'err', '/_ad');
        }

        $ad       = Model('Ad_Ad');
        $baseInfo = $ad->row(array('ad_id' => $adId));

        $this->assign("baseInfo", $baseInfo);

        $this->layout = 'ajax';
    }

    /**
     * 审核
     */
    public function examineOpAction()
    {
         // 把数据 插入审核表
        if ($res) {
            $db =  Com_DB::get('BingoMobi');
            $db->query("Insert into bg_ad_Info_examine SELECT * FROM `bg_ad_Info` where ad_id = {$adId};");
            $db->query("Insert into bg_ad_collect_field_examine SELECT * FROM `bg_ad_collect_field` where ad_id = {$adId};");
            $db->query("Insert into bg_ad_app_type_examine SELECT * FROM `bg_ad_app_type` where ad_id = {$adId};");
        }
    }

}

